{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "node_x, node_k1, node_b1 = 'x', 'k1', 'b1'\n",
    "node_k2, node_b2 = 'k2', 'b2'\n",
    "node_linear_01, node_linear_02, \\\n",
    "node_sigmoid = 'linear_01', 'linear_02', 'sigmoid'\n",
    "node_loss = 'loss'\n",
    "\n",
    "graph = { # represent model\n",
    "    node_x: [node_linear_01],\n",
    "    node_k1: [node_linear_01],\n",
    "    node_b1: [node_linear_01],\n",
    "    node_linear_01: [node_sigmoid],\n",
    "    node_sigmoid: [node_linear_02],\n",
    "    node_k2: [node_linear_02],\n",
    "    node_b2: [node_linear_02],\n",
    "    node_linear_02: [node_loss],\n",
    "}\n",
    "         # for n in graph:\n",
    "         #    all_inputs += graph[n]\n",
    "         #    all_outputs.append(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['b1', 'x', 'k1', 'b2', 'linear_01', 'sigmoid', 'k2', 'linear_02', 'loss']\n"
     ]
    }
   ],
   "source": [
    "# for i in graph:\n",
    "#     print(i)\n",
    "#     print(graph[i])\n",
    "#\n",
    "# print('next one')\n",
    "#\n",
    "# for i,j in graph.items():\n",
    "#     print(i)\n",
    "#     print(j)\n",
    "\n",
    "import random\n",
    "def toplogic(graph):\n",
    "\n",
    "    sorted_graphy=[]\n",
    "\n",
    "    while len(graph)>0:\n",
    "\n",
    "        node_input=[]\n",
    "        node_output=[]\n",
    "\n",
    "        for n in graph:\n",
    "            node_input += graph[n]\n",
    "            node_output.append(n)\n",
    "\n",
    "\n",
    "\n",
    "        node_input=set(node_input)\n",
    "        node_output=set(node_output)\n",
    "\n",
    "        remove_node=node_output - node_input\n",
    "\n",
    "        if len(remove_node)>0:\n",
    "            node=random.choice(list(remove_node))\n",
    "            sorted_graphy.append(node)\n",
    "            # visit=[node]\n",
    "            # print(node)\n",
    "            if len(graph)==1:\n",
    "                sorted_graphy += graph[node]\n",
    "\n",
    "            graph.pop(node)\n",
    "            # sorted_graphy+=visit\n",
    "\n",
    "        else:\n",
    "            break\n",
    "\n",
    "\n",
    "    return sorted_graphy\n",
    "print(toplogic(graph))\n",
    "# import random\n",
    "# def toplogic(graph):\n",
    "#     sorted_node = []\n",
    "#\n",
    "#     while len(graph) > 0:\n",
    "#\n",
    "#         all_inputs = []\n",
    "#         all_outputs = []\n",
    "#\n",
    "#         for n in graph:\n",
    "#             all_inputs += graph[n]\n",
    "#             all_outputs.append(n)\n",
    "#\n",
    "#         all_inputs = set(all_inputs)\n",
    "#         all_outputs = set(all_outputs)\n",
    "#\n",
    "#         need_remove = all_outputs - all_inputs  # which in all_inputs but not in all_outputs\n",
    "#\n",
    "#         if len(need_remove) > 0:\n",
    "#             node = random.choice(list(need_remove))\n",
    "#\n",
    "#             visited_next = [node]\n",
    "#             if len(graph) == 1:  visited_next += graph[node]\n",
    "#\n",
    "#             graph.pop(node)\n",
    "#             sorted_node += visited_next\n",
    "#\n",
    "#             # for _, links in graph.items():\n",
    "#             #     if node in links: links.remove(node)\n",
    "#         else:\n",
    "#             break\n",
    "#\n",
    "#     return sorted_node\n",
    "# a=['k']\n",
    "# b=['j']\n",
    "# print(a+b)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "pycharm-a2eace9b",
   "language": "python",
   "display_name": "PyCharm (neuralnrtwork)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}